-- Unity集成配置文件
-- 定义Unity与Lua Battle System集成的相关配置

local UnityIntegrationConfig = {
    -- Unity项目配置
    unity_project = {
        project_name = "LuaBattleUnity",
        version = "1.0.0",
        target_platform = "PC", -- PC, Mobile, Console
        render_pipeline = "URP", -- URP, HDRP, Built-in
        
        -- XLua配置
        xlua = {
            version = "2.1.15",
            lua_version = "lua53", -- lua53, luajit
            enable_hotfix = true,
            enable_reflection = true,
            gc_mode = "incremental" -- incremental, generational
        }
    },
    
    -- 性能配置
    performance = {
        max_battles_per_frame = 1, -- 每帧最大处理战斗数
        battle_update_interval = 0.016, -- 战斗更新间隔(秒)
        ui_update_interval = 0.033, -- UI更新间隔(秒)
        max_concurrent_animations = 10, -- 最大并发动画数
        
        -- 内存配置
        memory = {
            max_lua_memory_mb = 128, -- 最大Lua内存使用(MB)
            gc_threshold_mb = 64, -- GC触发阈值(MB)
            gc_step_kb = 1024 -- GC步长(KB)
        }
    },
    
    -- UI配置
    ui = {
        -- 分辨率配置
        resolution = {
            reference_width = 1920,
            reference_height = 1080,
            match_mode = "width" -- width, height, both
        },
        
        -- 动画配置
        animation = {
            ui_transition_duration = 0.3, -- UI过渡动画时长
            character_animation_speed = 1.0, -- 角色动画速度
            effect_animation_speed = 1.0, -- 特效动画速度
            damage_number_duration = 2.0, -- 伤害数字显示时长
            skill_effect_duration = 1.5 -- 技能特效时长
        },
        
        -- 颜色配置
        colors = {
            damage_normal = Color(1.0, 0.2, 0.2, 1.0), -- 普通伤害颜色
            damage_critical = Color(1.0, 1.0, 0.0, 1.0), -- 暴击伤害颜色
            heal = Color(0.2, 1.0, 0.2, 1.0), -- 治疗颜色
            buff = Color(0.2, 0.8, 1.0, 1.0), -- 增益效果颜色
            debuff = Color(0.8, 0.2, 0.8, 1.0), -- 减益效果颜色
            selected = Color(1.0, 1.0, 0.0, 1.0), -- 选中颜色
            disabled = Color(0.5, 0.5, 0.5, 1.0) -- 禁用颜色
        }
    },
    
    -- 音频配置
    audio = {
        master_volume = 1.0,
        bgm_volume = 0.8,
        sfx_volume = 0.9,
        voice_volume = 0.7,
        
        -- 音频池配置
        audio_pool = {
            max_bgm_channels = 2,
            max_sfx_channels = 16,
            max_voice_channels = 8
        }
    },
    
    -- 输入配置
    input = {
        -- 键盘输入
        keyboard = {
            confirm = "space",
            cancel = "escape",
            up = "w",
            down = "s",
            left = "a",
            right = "d",
            skill_1 = "1",
            skill_2 = "2",
            skill_3 = "3",
            skill_4 = "4"
        },
        
        -- 鼠标输入
        mouse = {
            left_click = 0,
            right_click = 1,
            middle_click = 2,
            scroll_sensitivity = 1.0
        },
        
        -- 手柄输入
        gamepad = {
            confirm = "buttonSouth", -- A键
            cancel = "buttonEast", -- B键
            up = "leftStick",
            down = "leftStick",
            left = "leftStick",
            right = "leftStick",
            skill_1 = "buttonWest", -- X键
            skill_2 = "buttonNorth", -- Y键
            skill_3 = "leftShoulder",
            skill_4 = "rightShoulder"
        }
    },
    
    -- 调试配置
    debug = {
        enable_debug_mode = false,
        enable_lua_console = false,
        enable_performance_monitor = false,
        enable_event_logger = false,
        
        -- 日志配置
        logging = {
            log_level = "INFO", -- DEBUG, INFO, WARN, ERROR
            max_log_files = 10,
            max_log_file_size_mb = 10,
            enable_file_logging = true,
            enable_console_logging = true
        }
    },
    
    -- 网络配置（预留）
    network = {
        enable_network = false,
        server_url = "localhost:8080",
        connection_timeout = 30.0,
        heartbeat_interval = 10.0,
        max_reconnect_attempts = 3
    },
    
    -- 本地化配置
    localization = {
        default_language = "zh-CN",
        supported_languages = {"zh-CN", "en-US", "ja-JP"},
        fallback_language = "en-US",
        enable_auto_detect = true
    },
    
    -- 资源配置
    resources = {
        -- 资源加载配置
        loading = {
            enable_async_loading = true,
            max_concurrent_loads = 4,
            preload_battle_resources = true,
            preload_ui_resources = true
        },
        
        -- 资源缓存配置
        cache = {
            enable_texture_cache = true,
            enable_audio_cache = true,
            max_cache_size_mb = 256,
            cache_cleanup_interval = 60.0
        }
    }
}

-- 获取配置值
function UnityIntegrationConfig.get(path, default_value)
    local keys = {}
    for key in string.gmatch(path, "[^%.]+") do
        table.insert(keys, key)
    end
    
    local current = UnityIntegrationConfig
    for _, key in ipairs(keys) do
        if current[key] then
            current = current[key]
        else
            return default_value
        end
    end
    
    return current
end

-- 设置配置值
function UnityIntegrationConfig.set(path, value)
    local keys = {}
    for key in string.gmatch(path, "[^%.]+") do
        table.insert(keys, key)
    end
    
    local current = UnityIntegrationConfig
    for i = 1, #keys - 1 do
        local key = keys[i]
        if not current[key] then
            current[key] = {}
        end
        current = current[key]
    end
    
    current[keys[#keys]] = value
end

-- 验证配置
function UnityIntegrationConfig.validate()
    local errors = {}
    
    -- 验证Unity项目配置
    if not UnityIntegrationConfig.unity_project then
        table.insert(errors, "Unity项目配置缺失")
    end
    
    -- 验证性能配置
    if not UnityIntegrationConfig.performance then
        table.insert(errors, "性能配置缺失")
    end
    
    -- 验证UI配置
    if not UnityIntegrationConfig.ui then
        table.insert(errors, "UI配置缺失")
    end
    
    return #errors == 0, errors
end

-- 导出配置到文件
function UnityIntegrationConfig.export_to_file(file_path)
    local file = io.open(file_path, "w")
    if not file then
        return false, "无法打开文件: " .. file_path
    end
    
    file:write("-- Unity集成配置文件\n")
    file:write("-- 自动生成，请勿手动修改\n\n")
    
    -- 使用JSON格式导出配置
    local json = require("utils.json")
    local config_json = json.encode(UnityIntegrationConfig)
    file:write("return " .. config_json)
    
    file:close()
    return true
end

-- 从文件导入配置
function UnityIntegrationConfig.import_from_file(file_path)
    local file = io.open(file_path, "r")
    if not file then
        return false, "无法打开文件: " .. file_path
    end
    
    local content = file:read("*all")
    file:close()
    
    -- 执行配置文件
    local func, err = load(content)
    if not func then
        return false, "配置文件格式错误: " .. err
    end
    
    local success, imported_config = pcall(func)
    if not success then
        return false, "配置文件执行错误: " .. imported_config
    end
    
    -- 合并配置
    for key, value in pairs(imported_config) do
        UnityIntegrationConfig[key] = value
    end
    
    return true
end

-- 重置为默认配置
function UnityIntegrationConfig.reset_to_default()
    -- 这里可以重新加载默认配置
    -- 暂时保持当前配置
end

-- 打印配置信息
function UnityIntegrationConfig.print_config()
    local Logger = require("utils.logger")
    Logger.info("=== Unity集成配置信息 ===")
    Logger.info("项目名称: " .. UnityIntegrationConfig.unity_project.project_name)
    Logger.info("版本: " .. UnityIntegrationConfig.unity_project.version)
    Logger.info("目标平台: " .. UnityIntegrationConfig.unity_project.target_platform)
    Logger.info("渲染管线: " .. UnityIntegrationConfig.unity_project.render_pipeline)
    Logger.info("XLua版本: " .. UnityIntegrationConfig.unity_project.xlua.version)
    Logger.info("Lua版本: " .. UnityIntegrationConfig.unity_project.xlua.lua_version)
    Logger.info("========================")
end

return UnityIntegrationConfig